
import BigNumber from "bignumber.js";
let BN = new BigNumber.clone()
BN.config({
    EXPONENTIAL_AT: [-999999, 999999]
})
// 加法
export function bigPlus(q, b, decimal = 50, zeroFilling = false) {
    decimal = Number(decimal);
    let n = BN(q);

    let result;

    if (!zeroFilling || decimal >= 50) {
        result = n.plus(b).dp(decimal, 1).toString();
    } else {
        result = n.plus(b).dp(decimal, 1);
        result = result.toFixed(decimal, 1).toString();
    }

    return result != 'NaN' && result != 'Infinity' ? result : 0;
}

// 减法
export function bigMinus(q, b, decimal = 50, zeroFilling = false) {
    decimal = Number(decimal);
    let n = BN(q);
    let result;

    if (!zeroFilling || decimal >= 50) {
        result = n.minus(b).dp(decimal, 1).toString();
    } else {
        result = n.minus(b).dp(decimal, 1);
        result = result.toFixed(decimal, 1).toString();
    }

    return result != 'NaN' && result != 'Infinity' ? result : 0;
}

// 乘法
export function bigTimes(q, b, decimal = 2, zeroFilling = false) {
    decimal = Number(decimal);
    let n = BN(q);
    let result;

    if (!zeroFilling || decimal >= 2) {
        result = n.times(b).dp(decimal, 1).toString();
    } else {
        result = n.times(b).dp(decimal, 1);
        result = result.toFixed(decimal, 1).toString();
    }

    return result != 'NaN' && result != 'Infinity' ? result : 0;
}

// 除法
export function bigDiv(q, b, decimal = 50, zeroFilling = false) {
    decimal = Number(decimal);
    let n = BN(q);

    let result;

    if (!zeroFilling || decimal >= 50) {
        result = n.div(b).dp(decimal, 1).toString();
    } else {
        // 保留小数位不足小数位自动补0
        result = n.div(b).dp(decimal, 1);
        result = result.toFixed(decimal, 1).toString();
    }

    return result != 'NaN' && result != 'Infinity' ? result : 0;
}

// 取余
export function bigMod(q, b, decimal = 50) {
    decimal = Number(decimal);
    let n = BN(q);
    let result = n.modulo(b).dp(decimal, 1).toString();
    return result != 'NaN' && result != 'Infinity' ? result : 0;
}

// 保留小数位，去掉多余的0
export function bigDecimalPlace(q, decimal = 50, zeroFilling = false) {
    decimal = Number(decimal);
    let n = BN(q);
    let result;

    if (!zeroFilling || decimal >= 50) {
        result = n.times(1).dp(decimal, 1).toString();
    } else {
        // 保留小数位不足小数位自动补0
        result = n.times(1).dp(decimal, 1);
        result = result.toFixed(decimal, 1).toString();
    }

    return result != 'NaN' && result != 'Infinity' ? result : 0;
}

// 输入的时候过滤掉非数字字符，以及保留小数位
export function decimalPlace(number, decimal = 4) {
    decimal = Number(decimal);
    number = String(number);
    number = number.replace(/[^\d.]/g, ""); // 清除"数字"和"."以外的字符
    number = number.replace(/^\./g, ""); // 验证第一个字符是数字
    number = number.replace(/\.{2,}/g, "."); // 只保留第一个, 清除多余的
    number = number.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    if (decimal == 0) {
        number = number.replace(/^0(0+)|[^\d]+/g, '');// 只能整数
    } else {
        // 保存n个小数
        let q = '^(\\-)*(\\d+)\\.(\\d{';
        let n = `${decimal}}).*$`;
        let text = q + n;
        number = number.replace(new RegExp(text), '$1$2.$3');         // 只能输入n个小数
    }
    if (number.indexOf(".") < 0 && number != "") {// 此处控制的是以上如果没有小数点，首位不能为类似于 01、02的数值
        number = number.replace(/^0+(?!$)/, '');
    }
    number = isNaN(number) ? 0 : number; // 不让显示NAN
    return number;
}